package com.dci.service.impl.schedule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dci.context.Config;
import com.dci.dao.pms.project.PmsItemInputMapper;
import com.dci.dao.pms.schedule.ProgressEnteringMapper;
import com.dci.dao.pms.schedule.ProgressTrackMapper;
import com.dci.model.schedule.ProgressEntering;
import com.dci.result.JsonData;
import com.dci.result.PageResult;
import com.dci.result.ResultInfo;
import com.dci.result.ResultUtil;
import com.dci.service.schedule.ProgressEntertingService;
import com.dci.util.UUIDBuild;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.schedule
 * @Description: 业务录入
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class ProgressEnteringServiceImpl implements ProgressEntertingService {

    @Resource
    private ProgressEnteringMapper progressEnteringMapper;

    @Resource
    private ProgressTrackMapper progressTrackMapper;

    @Resource
    private PmsItemInputMapper pmsItemInputMapper;

    /**
     * 新增
     *
     * @param progressEntering
     * @return
     */
    @Override
    public ResultInfo insert(ProgressEntering progressEntering) {
        progressEntering.setId(UUIDBuild.getUUID());
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(now);
        progressEntering.setAmount_time(date);
        int i = progressEnteringMapper.insert(progressEntering);
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
    }

    /**
     * 查询所有
     *
     * @param progressEntering
     * @return
     */
    @Override
    public PageResult select(ProgressEntering progressEntering) {
        PageHelper.startPage(progressEntering.getPageNum(), progressEntering.getPageSize());
        List<ProgressEntering> list = progressEnteringMapper.select(progressEntering);
        PageInfo<ProgressEntering> pageInfo = new PageInfo<>(list);
        Long count = pageInfo.getTotal();
        return PageResult.success(list, null, null, count);
    }

    /**
     * 通过code查询
     *
     * @param code
     * @return
     */
    @Override
    public JsonData selectByCode(String code) {
        List<ProgressEntering> data = progressEnteringMapper.selectByCode(code);
        //long count = data.size();
        return JsonData.success(data, 906);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public JsonData selectById(String id) {
        List<ProgressEntering> data = progressEnteringMapper.selectById(id);
        //long count = data.size();
        return JsonData.success(data, 906);
    }

    /**
     * 修改
     *
     * @param progressEntering
     * @return
     */
    @Override
    public JsonData update(ProgressEntering progressEntering) {
        Date date = new Date();
        //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // 格式转换.String
        //String dateTime = df.format(date);
        //df.format(date);
//        progressEntering.setEntering_time(date);
        progressEntering.setAudit_status(0);
        int i = progressEnteringMapper.update(progressEntering);
        if (i > 0) {
            JsonData.success(progressEntering, 906);
        }
        return JsonData.fail(911);
    }

    /**
     * 资金拨付并添加更新凭证文件
     *
     * @param progressEntering
     * @return
     */
    @Override
    public JsonData insertAdd(ProgressEntering progressEntering) {
        progressEntering.setId(UUIDBuild.getUUID());
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(now);
        progressEntering.setAmount_time(date);
        int i = progressEnteringMapper.insertAdd(progressEntering);
        if (i > 0) {
            return JsonData.success(progressEntering, 906);
        }
        return JsonData.success(progressEntering, 911);
//            if (i > 0) {
//                int r = 0;
//                //更新资金拨付使用情况
//                List<ProgressTrack> progressTracks = progressTrackMapper.selectByCode(progressEntering.getCode());
//                if(!progressTracks.isEmpty()){
//                    ProgressTrack progressTrack = progressTracks.get(0);
//                    if(!StringUtils.isEmpty(progressTrack.getFinance_amount()))
//                    {
//                        Double financeAmount =  Double.parseDouble(progressTrack.getFinance_amount());
//                        financeAmount+=Double.parseDouble(progressEntering.getItem_amount());
//                        progressTrack.setFinance_amount(financeAmount.toString());
//                        r = progressTrackMapper.update(progressTrack);
//                    }else{
//                        progressTrack.setFinance_amount(progressEntering.getItem_amount());
//                        r = progressTrackMapper.update(progressTrack);
//                    }
//                }else{
//                    ProgressTrack progressTrack = new ProgressTrack();
//                    progressTrack.setFinance_amount(progressEntering.getItem_amount());
//                    progressTrack.setCode(progressEntering.getCode());
//                    progressTrack.setConstruction_unit(progressEntering.getConstruction_unit());
//                    progressTrack.setContent(progressEntering.getContent());
//                    progressTrack.setName(progressEntering.getName());
//                    progressTrack.setItem_amount(progressEntering.getItem_amount());
//                    progressTrack.setProject_team(progressEntering.getProject_team());
//                    progressTrack.setRemarks(progressEntering.getRemarks());
//                    r = progressTrackMapper.insert(progressTrack);
//                }
//                if(r>0) {
//                    return JsonData.success(progressEntering, 906);
//                }else
//                {
//                    return JsonData.fail(911);
//                }
//            }


    }

    /**
     * 审核
     *
     * @param id
     * @param audit_status
     * @param verify_opinion
     * @return
     */
    @Override
    public ResultInfo audit(String id, int audit_status, String verify_opinion) {
        ProgressEntering progressEntering = new ProgressEntering();
        Date date = new Date();
        //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // 格式转换.String
        //String dateTime = df.format(date);
        //df.format(date);
//        progressEntering.setAudit_time(date);
        progressEntering.setAudit_status(audit_status);
//        progressEntering.setVerify_opinion(verify_opinion);
        int i = progressEnteringMapper.update(progressEntering);
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
    }

    /**
     * 删除
     *
     * @param list
     * @return
     */
    @Override
    public PageResult delete(List<ProgressEntering> list) {
        if (list != null && list.size() != 0) {
            for (ProgressEntering progressEntering : list) {
                // 1为删除状态
                progressEntering.setDel_flag("1");
                progressEnteringMapper.update(progressEntering);
            }
        }
        // 删除后重新查询返回数据列表
        PageHelper.startPage(1, 10);
        list = progressEnteringMapper.select(new ProgressEntering());
        PageInfo<ProgressEntering> pageInfo = new PageInfo<>(list);
        Long count = pageInfo.getTotal();
        return PageResult.success(list, null, null, count);
    }



    @Override
    public List<ProgressEntering> selectAll() {
        return progressEnteringMapper.selectAll();
    }


    /**
     * 导入数据
     *
     * @param simple
     * @param progressEntering
     * @return
     */
    @Override
    public ResultInfo json(String simple, String progressEntering) {
        List<String> keys = pmsItemInputMapper.selectColumnKeyResult();
        List<String> values = pmsItemInputMapper.selectColumnValueResult();
        JSONArray array = JSONArray.parseArray(simple);
        for (int i = 0; i < array.size(); i++) {
            String value = String.valueOf(array.get(i));
            Map map = (Map) JSON.parse(value);
            Set<String> set = map.keySet();
//            String code = "";
            String key1 = "";
            String values1 = "";
            String sql = "";
            for (String key : set) {
                if (values.contains(key)) {
                    String val = String.valueOf(map.get(key));
//                    if (keys.get(values.indexOf(key)).equals("code")) {
//                        code = progressEntering + String.valueOf(map.get(key));
//                        val = code;
//                    }
                    if (key1.equals("")) {
                        key1 += keys.get(values.indexOf(key));
                    } else {
                        key1 += "," + keys.get(values.indexOf(key));
                    }
                    if (values1.equals("")) {
                        values1 += "'" + val + "'";
                    } else {
                        values1 += ",'" + val + "'";
                    }
                }
            }
//            List<PmsItemInput> list = pmsItemInputMapper.selectByCode(code);
//            if (list.size() == 0) {
                String id = UUIDBuild.getUUID();
                String auditStatus = "3";
                Date now = new Date();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = df.format(now);
                sql = "insert into pms_item_input(id,auditStatus,time," + key1 + ") values ('" + id + "'," + auditStatus + ",'" + time + "'," + values1 + ")";
                int sql1 =   pmsItemInputMapper.insertObjectBatch(sql);
                if(sql1 > 0){
                    return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
                }
            return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
//            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }


}